package cn.sinata.wanglin.village.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.xilada.xldutils.activitys.TitleActivity;
import com.xilada.xldutils.utils.ActivityUtil;
import com.xilada.xldutils.utils.SharedPreferencesUtils;
import com.xilada.xldutils.utils.TimeUtils;
import com.xilada.xldutils.utils.Utils;

import cn.sinata.wanglin.R;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.model.NewsinfoDetails;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;
import cn.sinata.wanglin.utils.RelativeDateFormat;
import cn.sinata.wanglin.utils.ShareConfig;
import cn.sinata.wanglin.utils.ShareUtils;

/**
 * 小区动态---新闻资讯详情页面
 */
public class NewsinfoDetailsActivity extends TitleActivity {

    private WebView webView;
    private View loadingView;
    private TextView tv_title;
    private TextView tv_fromandtiem;
    private TextView tv_gotocomment;
    private TextView tv_gotocommentlist;
    private String id;
    private String title="开心网邻";
    @Override
    protected int setContentId() {
        return R.layout.activity_newsinfodetails;

    }
    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void initView() {
        super.initView();
        setTitle("资讯详情");
        id = getIntent().getStringExtra("id");
        tv_gotocomment= bind(R.id.tv_gotocomment);
        tv_gotocommentlist= bind(R.id.tv_gotocommentlist);
        webView = bind(R.id.webView);
        loadingView = bind(R.id.ll_loading);
        tv_title = bind(R.id.tv_title);
        tv_fromandtiem = bind(R.id.tv_fromandtiem);
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setDefaultTextEncodingName("utf-8");
        tv_gotocomment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    ActivityUtil.create(mContext)
                            .go(EditCommentOtherActivity.class)
                            .put("id", id)
                            .put("toUser", "-1")
                            .put("externalType", "F")
                            .startForResult(12);
            }
        });
        tv_gotocommentlist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ActivityUtil.create(mContext).go(NewinfoCommentListActivity.class).put("id",id)
                        .put("type","F")
                        .start();
            }
        });
        addRightButton(R.mipmap.ic_shareto, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String url= ShareConfig.shareToApp("4",id);
                ShareUtils.share(NewsinfoDetailsActivity.this,title,"开心网邻",url);
            }
        });
        getData(id);

    }

    protected boolean resetTitle(){
        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //webView回收,部分手机无效
        if (webView!=null){
            webView.stopLoading();
            webView.removeAllViews();
            webView.destroy();
        }
        if (loadingView !=null)
            loadingView = null;
    }
    private void showUrl(String urlstr){
        webView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                if (resetTitle()){
                    setTitle(title);
                }
            }

        });

        webView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                if (loadingView !=null)
                    loadingView.setVisibility(View.GONE);
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                if (loadingView !=null)
                    loadingView.setVisibility(View.VISIBLE);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                if (!TextUtils.isEmpty(url)) {
                    if (url.startsWith("joinuser")){
                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                        startActivity(intent);
                    }else {
                        webView.loadUrl(url);
                    }

                }
                return true;
            }
        });
        webView.loadUrl(urlstr);
    }
    private void getData(String id){
        String userId= SharedPreferencesUtils.getString(Constant.SP_USERID);
        HttpManager.getNewsinfoDetail(userId, id,0).subscribe(new ResultDataSubscriber<NewsinfoDetails>(this) {
            @Override
            public void onSuccess(String msg, NewsinfoDetails newsinfoDetails) {
                if(!TextUtils.isEmpty(newsinfoDetails.getCommentCount())){
                    tv_gotocommentlist.setText(newsinfoDetails.getCommentCount());
                }
                if(!TextUtils.isEmpty(newsinfoDetails.getNewsTitle())){
                    title=newsinfoDetails.getNewsTitle();
                }
                if("1".equals(newsinfoDetails.getNewsType())){
                    if (loadingView !=null)
                        loadingView.setVisibility(View.GONE);
                    if (!TextUtils.isEmpty(newsinfoDetails.getNewsTitle())){
                        tv_title.setText(newsinfoDetails.getNewsTitle());
                        tv_title.setVisibility(View.VISIBLE);
                    }else{
                        tv_title.setVisibility(View.GONE);
                    }
                    tv_fromandtiem.setVisibility(View.VISIBLE);
                    String  createtime = TimeUtils.getMilliTimeYMD(newsinfoDetails.getCreateDate());
                    if(!TextUtils.isEmpty(newsinfoDetails.getNewsOut())){
                        tv_fromandtiem.setText(newsinfoDetails.getNewsOut()+"\u3000"+createtime);
                    }else{
                        tv_fromandtiem.setText(createtime);
                    }
                   webView.loadDataWithBaseURL(null,newsinfoDetails.getNewsInfo(), "text/html", "utf-8", null);

                }else{
                    showUrl(newsinfoDetails.getNewsUrl());
                }

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK){
            switch (requestCode){
                case 12:
                    finish();
                    break;
            }
        }
    }
}
